home *** CD-ROM | disk | FTP | other *** search
-
-
- /*
- File: RenameGXHeaders.c
-
- Contains: MPW tool to replace old header names with new header names.
- Omits multiple inclusions of the same new header.
-
- Version 1.0
-
- Written by: John Daggett, with parts taken from the Skia preprocess tool
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <3> 7/7/95 jtd revving for changes in GX printing headers
- <2> 4/5/95 jtd fixed comments a bit
- <1> 4/5/95 jtd First checked in.
- */
-
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <CursorCtl.h>
-
- typedef enum { false, true } boolean;
-
- void fprintTrueFalse (FILE *fp, boolean trueOrFalse) {
- if ( trueOrFalse )
- fprintf (fp, "True");
- else
- fprintf (fp, "False");
- }
-
- typedef struct {
- boolean alreadyReplaced; /* to avoid multiple inclusions of the same file */
- boolean system; /* need to transform "" to <> for system headers */
- char newName[32]; /* name with no spaces */
- } NewHeader;
-
- typedef struct {
- char oldName[32]; /* name with spaces */
- int newIndex; /* index into table of new names */
- } OldHeader;
-
- /* new GX header names - no spaces and prefixed with GX */
- /* Warning: oldxxxx[] must be kept in sync with new[]!!!! */
- NewHeader new[] = {
- { false, true, "GXEnvironment.h" }, /* 0 */
- { false, true, "GXErrors.h" }, /* 1 */
- { false, true, "GXFonts.h" }, /* 2 */
- { false, true, "GXGraphics.h" }, /* 3 */
- { false, true, "GXLayout.h" }, /* 4 */
- { true, true, "GXLinkage.h" }, /* 5 */ /* no longer needed! */
- { false, true, "GXMath.h" }, /* 6 */
- { false, true, "GXTypes.h" }, /* 7 */
- { false, true, "GXGraphicsPriv.h" }, /* 8 */
- { false, true, "ScalerPriv.h" }, /* 9 */
- { false, true, "ScalerTypes.h" }, /* 10 */
- { false, true, "SFNTTypes.h" }, /* 11 */
- { false, false, "CameraLibrary.h" }, /* 12 */
- { false, false, "ColorLayoutLibrary.h" }, /* 13 */
- { false, false, "FontLibrary.h" }, /* 14 */
- { false, false, "FontMenuLibrary.h" }, /* 15 */
- { false, false, "GraphicsLibraries.h" }, /* 16 */
- { false, false, "GraphicsStateLibrary.h" }, /* 17 */
- { false, false, "GraphicsToolboxLibrary.h" }, /* 18 */
- { false, false, "GraphicsBugLibrary.h" }, /* 19 */
- { false, false, "LayoutEditLibrary.h" }, /* 20 */
- { false, false, "LayoutLibrary.h" }, /* 21 */
- { false, false, "OffscreenLibrary.h" }, /* 22 */
- { false, false, "QDLibrary.h" }, /* 23 */
- { false, false, "ScalerLibrary.h" }, /* 24 */
- { false, false, "SelectionLibrary.h" }, /* 25 */
- { false, false, "ShapeControlsLibrary.h" }, /* 26 */
- { false, false, "StorageLibrary.h" }, /* 27 */
- { false, false, "TrueTypeLibrary.h" }, /* 28 */
- { false, false, "LayoutFeatureConstants.h" }, /* 29 */
- { false, true, "GXPrinterDrivers.h" }, /* 30 */
- { false, true, "GXPrinting.h" }, /* 31 */
- { false, true, "GXMessages.h" } /* 32 */
- };
-
- /* original GX header names with spaces */
- /* last entry is empty as a flag to stop searching */
- OldHeader oldWithSpaces[] = {
- { "graphics errors.h", 1 },
- { "graphics debugging.h", 1 },
- { "graphics toolbox.h", 0 },
- { "graphics macintosh.h", 0 },
- { "math types.h", 6 },
- { "math routines.h", 6 },
- { "font routines.h", 2 },
- { "graphics routines.h", 3 },
- { "layout routines.h", 4 },
- { "font types.h", 7 },
- { "layout types.h", 7 },
- { "graphics types.h", 7 },
- { "graphics stream types.h", 7 },
- { "synonym tags.h", 7 },
- { "graphics linkage.h", 5 },
- { "private graphics routines.h", 8 },
- { "scaler components.h", 9 },
- { "scaler routines.h", 9 },
- { "scaler types.h", 10 },
- { "sfnt types.h", 11 },
- { "camera library.h", 12 },
- { "color layout library.h", 13 },
- { "font library.h", 14 },
- { "font menu library.h", 15 },
- { "graphics libraries.h", 16 },
- { "graphics state library.h", 17 },
- { "graphics toolbox library.h", 18 },
- { "graphicsBug library.h", 19 },
- { "layout edit library.h", 20 },
- { "layout library.h", 21 },
- { "offscreen library.h", 22 },
- { "qd library.h", 23 },
- { "scaler library.h", 24 },
- { "selection library.h", 25 },
- { "shape controls library.h", 26 },
- { "storage library.h", 27 },
- { "truetype library.h", 28 },
- { "PrintingDrivers.h", 30 },
- { "PrintingExtensions.h", 30 },
- { "PrintingManager.h", 31 },
- { "PrintingMessages.h", 31 },
- { "PrintingErrors.h", 31 },
- { "PrintingResTypes.h", 31 },
- { "Messages.h", 32 },
- { "", -1 }
- };
-
- /* original GX headers without spaces in the name */
- /* only needed for Apple internal use */
- OldHeader oldNoSpaces[] = {
- { "GraphicsErrors.h", 1 },
- { "GraphicsDebugging.h", 1 },
- { "GraphicsToolbox.h", 0 },
- { "GraphicsMacintosh.h", 0 },
- { "MathTypes.h", 6 },
- { "MathRoutines.h", 6 },
- { "FontRoutines.h", 2 },
- { "GraphicsRoutines.h", 3 },
- { "LayoutRoutines.h", 4 },
- { "FontTypes.h", 7 },
- { "LayoutTypes.h", 7 },
- { "GraphicsTypes.h", 7 },
- { "GraphicsStreamTypes.h", 7 },
- { "SynonymTags.h", 7 },
- { "GraphicsLinkage.h", 5 },
- { "PrivateGraphicsRoutines.h", 8 },
- { "TranslatorRoutines.h", 8 },
- { "ScalerComponents.h", 9 },
- { "ScalerRoutines.h", 9 },
- { "", -1 }
- };
-
- /* by default, convert names with spaces (use -i option to change) */
- OldHeader *old = oldWithSpaces;
-
- #define kMaxLineLength 1024
- #define kSpinTime 7
- #define kIncludeString "#include"
- #define kIncludeLength 8
- #define kIfString "#if"
- #define kEndifString "#endif"
-
- #define equalstring(a, b) ( strncmp((a), (b), strlen(b)) == 0 )
-
- /* default options */
- boolean gQuiet = false, /* output replacements and omissions */
- gOverwrite = false; /* write over input file */
- gSystem = true, /* change "" includes to <> includes for certain headers */
- gInternal = false; /* converting from names without spaces */
-
- /* globals */
- boolean gSkipping = false, gNesting = false;
-
- /* match - skip over whitespace before trying to match a string */
- boolean match (char *s, char *match) {
- while (isspace(*s)) s++;
- if ( (equalstring(s, match)) ) return true;
- return false;
- }
-
- /* replace an old GX header name with a new one */
- /* <whitespace>* #include <whitespace>* ['"' | '<'] <header name> ['"' | '>'] <any char>* */
- boolean replaceInclude (char *s, FILE *out) {
- char *start = s, *begin, *end, left, right;
- OldHeader *h = old;
-
- /* white space before #include */
- while (isspace(*s)) s++;
-
- if ( !(equalstring(s, kIncludeString)) ) {
- fputs (start, out);
- return false;
- }
-
- s += kIncludeLength;
- while (isspace(*s)) s++;
-
- /* read until the " or < */
- if ( *s != '\"' && *s != '<' ) {
- fputs (start, out);
- return false;
- }
- left = *s;
- begin = ++s;
-
- while ( *s != '\"' && *s != 0 && *s != '>' ) s++;
- if ( *s == 0 ) {
- fputs (start, out);
- return false;
- }
- right = *s;
- end = ++s;
-
- /* linear search for the old name */
- while ( *h->oldName != 0 ) {
- if ( equalstring(begin, h->oldName) ) {
-
- /* found the name, now replace it with the new name */
- *(begin - 1) = *(end - 1) = 0;
- if ( new[h->newIndex].system ) {
- left = '<';
- right = '>';
- }
- if ( new[h->newIndex].alreadyReplaced ) {
- if ( !gQuiet ) fprintf (stderr, "\tskipped replacing '%s' with %s\n", begin, new[h->newIndex].newName);
- gSkipping = true;
- } else {
- if ( !gQuiet ) fprintf (stderr, "\treplaced '%s' with %s\n", begin, new[h->newIndex].newName);
- new[h->newIndex].alreadyReplaced = true;
- fputs (start, out);
- fputc (left, out);
- fputs (new[h->newIndex].newName, out);
- fputc (right, out);
- fputs (end, out);
- }
- return true;
- }
- h++;
- }
-
- fputs (start, out);
- return false;
- }
-
-
- /* Note: nested if's present a problem, for when the inclusion of a header
- file is within an if, it may be appropriate to leave multiple
- #include's of the same file.
-
- Example:
-
- #if blah
- #include "graphics types.h"
- #else
- #include "layout types.h"
- #endif
-
- Since both will map to GXTypes.h, omitting the second #include will result
- in incorrect code. Rather than try and create a much fancier tool, the user
- of this tool should always verify the output of this tool when #include's
- are surrounded by #ifdef's and skipping occurs.
-
- This tool can warn the user when #if statements are present but this
- is a hack and not a real solution to the problem.
- */
-
- /* processData - read in lines from a file, keeping track of nested if's */
- void processData (FILE *in, FILE *out) {
- int line = 0;
- char buffer[kMaxLineLength];
- static int nestingLevel = 0, replacedNesting = -1;
-
- while( fgets(buffer,sizeof(buffer),in) ) {
- if ( replaceInclude (buffer, out) ) {
- if ( replacedNesting == -1 ) {
- replacedNesting = nestingLevel;
- } else if ( replacedNesting != nestingLevel ) {
- gNesting = true;
- }
- } else if ( match(buffer, kIfString) ) {
- nestingLevel++;
- } else if ( match(buffer, kEndifString) ) {
- nestingLevel--;
- }
- }
- }
-
- void printUsageAndExit (char *progname) {
- #ifdef NOSPACESOPTION
- fprintf(stderr, "usage: %s [-iqsw] [input] [-o output]\n", progname);
- #else
- fprintf(stderr, "usage: %s [-qsw] [input] [-o output]\n", progname);
- #endif
- fprintf(stderr, "where\n");
- #ifdef NOSPACESOPTION
- fprintf(stderr, "\t\t\t-i\tconvert from names with no spaces (Default: ");
- fprintTrueFalse(stderr, gInternal);
- fprintf(stderr, ")\n");
- #endif
- fprintf(stderr, "\t\t\t-q\tquiet mode (Default: ");
- fprintTrueFalse(stderr, gQuiet);
- fprintf(stderr, ")\n");
-
- fprintf(stderr, "\t\t\t-s\tinsert <> around system headers (Default: ");
- fprintTrueFalse(stderr, gSystem);
- fprintf(stderr, ")\n");
-
- fprintf(stderr, "\t\t\t-w\twrite over input file (Default: ");
- fprintTrueFalse(stderr, gOverwrite);
- fprintf(stderr, ")\n");
-
- fprintf(stderr, "\n");
- exit(1);
- }
-
- main(int argc, char *argv[])
- {
- FILE *in = stdin, *out = stdout;
- char *progname = argv[0], *p, *infile, buffer[kMaxLineLength];
-
- argv++;
- while (--argc > 0) {
- p = *argv++;
-
- if (*p == '-') {
- switch (p[1]) {
-
- /* output filename */
- case 'o':
- if (--argc < 0) printUsageAndExit(progname);
- p = *argv++;
- out = fopen(p, "w");
- if (!out) {
- fprintf(stderr, "%s: couldn't open %s\n", progname, p);
- return 1;
- }
- break;
-
- /* run in quiet mode */
- case 'q':
- gQuiet = true;
- break;
-
- /* undocumented option - converts headers with no spaces in the name */
- case 'i':
- gInternal = true;
- old = oldNoSpaces;
- break;
-
- /* convert "" includes to <> includes where appropriate */
- case 's':
- gSystem = true;
- break;
-
- /* overwrite the input file */
- case 'w':
- gOverwrite = true;
- break;
-
- /* user needs help */
- case 'h':
- case 'u':
- case '?':
- default:
- printUsageAndExit(progname);
-
- }
-
- } else if (in == stdin) {
- infile = p;
- in = fopen(infile, "r");
- if (!in) {
- fprintf(stderr, "%s: couldn't open %s\n", progname, infile);
- return 1;
- }
- } else {
- if (out != stdout) printUsageAndExit(progname);
- out = fopen(p, "w");
- if (!out) {
- fprintf(stderr, "%s: couldn't open %s\n", progname, p);
- return 1;
- }
- }
- }
-
- if ( gOverwrite ) {
- if ( out != stdout ) printUsageAndExit(progname);
- out = tmpfile();
- if (!out) {
- fprintf(stderr, "%s: couldn't create a temporary file\n", progname);
- return 1;
- }
- }
-
- processData (in, out);
-
- if ( gOverwrite ) {
- rewind(out);
- in = freopen (infile, "w", in);
- if ( !in ) {
- fprintf (stderr, "%s: couldn't open %s for writing\n", progname, infile);
- return 1;
- }
- while( fgets(buffer,sizeof(buffer),out) ) fputs(buffer,in);
- }
-
- /* nested if checking turned off */
- #if NOSPACESOPTION
- if ( gNesting && gSkipping ) fprintf (stderr, "\tWARNING: Skipping in a file with nested if's!\n");
- #endif
- if ( gSkipping && !gQuiet ) {
- fprintf (stderr, "Note:\n");
- fprintf (stderr, " Since the GX headers have been consolidated, several old GX headers often map to one new\n");
- fprintf (stderr, " GX header. This tool omits multiple inclusion of the same header file. This may be \n");
- fprintf (stderr, " incorrect in some rare cases. For example,\n");
- fprintf (stderr, " \n");
- fprintf (stderr, " #ifdef usingJustFonts\n");
- fprintf (stderr, " #include \"font types.h\"\n");
- fprintf (stderr, " #else\n");
- fprintf (stderr, " #include \"graphics types.h\"\n");
- fprintf (stderr, " #endif\n");
- fprintf (stderr, " \n");
- fprintf (stderr, " Since both of these old headers map to GXTypes.h, the second #include line would be\n");
- fprintf (stderr, " omitted incorrectly. Be sure verify that this is not the case.\n");
- }
- return 0;
- }
-